package com.jltx.xq.relaxnewspro.utils;

import android.content.Context;

/**
 * Created by bo on 2017/5/7.
 */

public class DisplayUtil {
    public  static  int getStatusBarHeight(Context context){

        int statusBarHeight = 0;
        /**
         * 获取状态栏高度——方法1
         * */
        //获取status_bar_height资源的ID
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
        }

        if(statusBarHeight <= 0){
            /**
             * 获取状态栏高度——方法2
             * */
            try {
                Class<?> clazz = Class.forName("com.android.internal.R$dimen");
                Object object = clazz.newInstance();
                int height = Integer.parseInt(clazz.getField("status_bar_height")
                        .get(object).toString());
                statusBarHeight = context.getResources().getDimensionPixelSize(height);
            } catch (Exception e) {

            }
        }

        return statusBarHeight;

    }

    public static int dip2px(Context context, float dpValue){
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
